package com.hyip.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.demo.Write;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.hyip.domain.TopicsActivity;
import com.hyip.exception.DataValidateException;
import com.hyip.financialchannel.view.XhEditorImgResponse;
import com.hyip.mongo.datasource.MongoDBManager;
import com.hyip.service.PaginationService;
import com.hyip.service.TopicsActivitiesService;
import com.hyip.util.MongoDBUtil;
import com.hyip.view.PaginationView;
import com.hyip.web.view.AjaxResult;
import com.hyip.web.view.WebResult;

@Controller
@RequestMapping("/topics")
public class ImageController {

	private String[] imgSuffixs = new String[] { "jpg", "jpeg","png","bmp","gif" };
	
	private String validateImg(MultipartFile imgFile)  throws DataValidateException{
		if(imgFile == null){
			throw new DataValidateException("请上传图片");
		}
		String fileName = imgFile.getOriginalFilename();
		long size = imgFile.getSize();
		if(size > 1024 * 1024){
			throw new DataValidateException("图片不能超过1M");
		}
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
		if(!Arrays.asList(imgSuffixs).contains(suffix)){
			throw new DataValidateException("图片格式只能是".concat(imgSuffixs.toString()));
		}
		return suffix;
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/loadFile")
	public void loadFile(@RequestParam("id") String id, HttpServletResponse response) {
		ServletOutputStream responseOutputStream = null;
		try {
			Date now = new Date();
			String modDate = now.toGMTString();
			String expDate = new DateTime(now.getTime()).plusMinutes(30).toDate().toGMTString();
			response.setHeader("Last-Modified", modDate);
			response.setHeader("Expires", expDate);
			response.setHeader("Cache-Control", "no-cache");
			response.setHeader("Pragma", "Pragma");
			response.setContentType("image/png");
			responseOutputStream = response.getOutputStream();
			topicsActivitiesService.loadFile(id, responseOutputStream);
		} catch (DataValidateException e) {
			
		} catch (Exception e) {
		} finally {
			try {
				responseOutputStream.flush();
				responseOutputStream.close();
			} catch (IOException e) {
			}
		}
	}
	
	@RequestMapping("/uploadImg")
	public void uploadImg(MultipartHttpServletRequest request,HttpServletResponse response){
		try {
			MultipartFile img = request.getFile("imgFile");
			validateImg(img);
			response.setCharacterEncoding( "utf-8" );
			response.setHeader("Content-Type" , "text/html");
			PrintWriter out = response.getWriter();
			//存储图片，返回图片地址
			
			JSONObject obj = new JSONObject();
			obj.put("error", 0);
			obj.put("url", "返回图片地址");
			out.println(obj.toJSONString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DataValidateException e) {
			e.printStackTrace();
		}
	}
}
